package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursePlanDto;
import com.zzyl.dto.NursePlanProjectDto;
import com.zzyl.entity.NursePlan;
import com.zzyl.entity.NursePlanProject;
import com.zzyl.mapper.NursePlanMapper;
import com.zzyl.service.NursePlanProjectService;
import com.zzyl.service.NursePlanService;
import com.zzyl.vo.NursePlanProjectVo;
import com.zzyl.vo.NursePlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class NursePlanServiceImpl implements NursePlanService {

    @Autowired
    private NursePlanMapper nursePlanMapper;
    @Autowired
    private NursePlanProjectService nursePlanProjectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(NursePlanDto dto) {
        //将dto -> NursePlan
        NursePlan nursePlan = BeanUtil.toBean(dto, NursePlan.class);
        //设置状态
        nursePlan.setStatus(1);
        //新增计划表数据
        nursePlanMapper.insert(nursePlan);
        //获取计划表id
        Long planId = nursePlan.getId();
        log.info("计划表id:{}",planId);
        //新增中间表数据 调用service层 批量插入数据
        nursePlanProjectService.insert(dto.getProjectPlans(),planId);
    }

    @Override
    public PageResponse<NursePlanVo> selectByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //查询
        List<NursePlanVo> voList = nursePlanMapper.selectByPage(name,status);
        //强转
        Page<NursePlanVo> page = (Page<NursePlanVo>) voList;
        return PageResponse.of(page,NursePlanVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NursePlanVo selectById(Long id) {
        //查询计划表
        NursePlanVo planVo = nursePlanMapper.selectById(id);
        //查询中间表
        List<NursePlanProjectVo> list = nursePlanProjectService.findById(id);
        planVo.setProjectPlans(list);
        return planVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NursePlanDto dto) {
        //强转为entity
        NursePlan nursePlan = BeanUtil.toBean(dto, NursePlan.class);
        //根据id修改计划表
        nursePlanMapper.update(nursePlan);
        Long planId = dto.getId();
        //批量删除
        nursePlanProjectService.deleteBatch(planId);
        //增加中间表信息
        nursePlanProjectService.insert(dto.getProjectPlans(),planId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //删除计划表相关信息
        nursePlanMapper.delete(id);
        //删除中间表相关信息
        nursePlanProjectService.deleteBatch(id);
    }

    @Override
    public void choiceUpdate(Long id, Integer status) {
        //创建计划表对象
        NursePlan nursePlan = new NursePlan();
        //设置属性
        nursePlan.setId(id);
        nursePlan.setStatus(status);
        //更新状态
        nursePlanMapper.update(nursePlan);
    }
}
